// pages/member/msg/msg.js
var api = require('../../../utils/httpUtil.js');
const app = getApp();
Component({
  options: {
    addGlobalClass: true,
  },
  data: {
    tabCur: 0,
    scrollLeft: 0,
    tabList: ['未读', '已读'],
    openId: null,
    imgUrl: api.imgUrl,
    unReadMsgList: [],
    readedMsgList: []
  },
  lifetimes: {
    // 组件生命周期函数，在组件布局完成后执行，此时可以获取节点信息   
    ready: function () {
      // 在组件布局完成后执行的操作
      this.setData({
        openId: app.globalData.openId
      })
      this.loadData();
    }
  },
  methods: {
    tabSelect(e) {
      this.setData({
        tabCur: e.currentTarget.dataset.id,
      })
    },
    loadData() {
      api.httpRequest(api.msgAPI + "/" + this.data.openId, {})
        .then(res => {
          if (res.code == 200) {
            var unReadList = res.data.filter(item => item.isRead === 0);
            unReadList.forEach(item => {
              item.date = item.createTime.substring(5, 10);
              item.time = item.createTime.substring(11, 16);
            });

            var readList = res.data.filter(item => item.isRead === 1);
            readList.forEach(item => {
              item.date = item.createTime.substring(5, 10);
              item.time = item.createTime.substring(11, 16);
            });

            this.setData({
              unReadMsgList: unReadList,
              readedMsgList: readList
            })
          }
        })
        .catch(error => {
          wx.showToast({
            title: error.message,
            duration: 1000,
            mask: true,
            icon: 'none'
          })
        });
    }
  }
})